home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch14 / Fractal.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-22  |  10KB  |  352 lines

  1. VERSION 5.00
  2. Begin VB.Form frmFractal 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Fractal"
  6.    ClientHeight    =   5295
  7.    ClientLeft      =   300
  8.    ClientTop       =   570
  9.    ClientWidth     =   9135
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5295
  24.    ScaleWidth      =   9135
  25.    Begin VB.CommandButton cmdDraw 
  26.       Caption         =   "Draw"
  27.       Default         =   -1  'True
  28.       Height          =   375
  29.       Left            =   1320
  30.       TabIndex        =   13
  31.       Top             =   480
  32.       Width           =   735
  33.    End
  34.    Begin VB.OptionButton optSurface 
  35.       Caption         =   "Valley"
  36.       Height          =   255
  37.       Index           =   6
  38.       Left            =   0
  39.       TabIndex        =   12
  40.       Top             =   3240
  41.       Width           =   2055
  42.    End
  43.    Begin VB.TextBox txtDy 
  44.       Height          =   285
  45.       Left            =   720
  46.       TabIndex        =   10
  47.       Text            =   "0.25"
  48.       Top             =   720
  49.       Width           =   495
  50.    End
  51.    Begin VB.TextBox txtLevel 
  52.       Height          =   285
  53.       Left            =   720
  54.       TabIndex        =   9
  55.       Text            =   "3"
  56.       Top             =   360
  57.       Width           =   495
  58.    End
  59.    Begin VB.CheckBox chkRemoveHidden 
  60.       Caption         =   "Remove Hidden"
  61.       Height          =   255
  62.       Left            =   240
  63.       TabIndex        =   7
  64.       Top             =   0
  65.       Value           =   1  'Checked
  66.       Width           =   1695
  67.    End
  68.    Begin VB.OptionButton optSurface 
  69.       Caption         =   "Mountain"
  70.       Height          =   255
  71.       Index           =   0
  72.       Left            =   0
  73.       TabIndex        =   6
  74.       Top             =   1080
  75.       Value           =   -1  'True
  76.       Width           =   2055
  77.    End
  78.    Begin VB.OptionButton optSurface 
  79.       Caption         =   "Hill"
  80.       Height          =   255
  81.       Index           =   1
  82.       Left            =   0
  83.       TabIndex        =   5
  84.       Top             =   1440
  85.       Width           =   2055
  86.    End
  87.    Begin VB.OptionButton optSurface 
  88.       Caption         =   "Ridge"
  89.       Height          =   255
  90.       Index           =   2
  91.       Left            =   0
  92.       TabIndex        =   4
  93.       Top             =   1800
  94.       Width           =   2055
  95.    End
  96.    Begin VB.OptionButton optSurface 
  97.       Caption         =   "Peaked Ridge"
  98.       Height          =   255
  99.       Index           =   3
  100.       Left            =   0
  101.       TabIndex        =   3
  102.       Top             =   2160
  103.       Width           =   2055
  104.    End
  105.    Begin VB.OptionButton optSurface 
  106.       Caption         =   "Rugged Ridge"
  107.       Height          =   255
  108.       Index           =   4
  109.       Left            =   0
  110.       TabIndex        =   2
  111.       Top             =   2520
  112.       Width           =   2055
  113.    End
  114.    Begin VB.OptionButton optSurface 
  115.       Caption         =   "Random"
  116.       Height          =   255
  117.       Index           =   5
  118.       Left            =   0
  119.       TabIndex        =   1
  120.       Top             =   2880
  121.       Width           =   2055
  122.    End
  123.    Begin VB.PictureBox picCanvas 
  124.       AutoRedraw      =   -1  'True
  125.       Height          =   5295
  126.       Left            =   2160
  127.       ScaleHeight     =   349
  128.       ScaleMode       =   3  'Pixel
  129.       ScaleWidth      =   461
  130.       TabIndex        =   0
  131.       Top             =   0
  132.       Width           =   6975
  133.    End
  134.    Begin VB.Label Label1 
  135.       Caption         =   "Dy"
  136.       Height          =   255
  137.       Index           =   1
  138.       Left            =   120
  139.       TabIndex        =   11
  140.       Top             =   720
  141.       Width           =   495
  142.    End
  143.    Begin VB.Label Label1 
  144.       Caption         =   "Level"
  145.       Height          =   255
  146.       Index           =   0
  147.       Left            =   120
  148.       TabIndex        =   8
  149.       Top             =   360
  150.       Width           =   495
  151.    End
  152. Attribute VB_Name = "frmFractal"
  153. Attribute VB_GlobalNameSpace = False
  154. Attribute VB_Creatable = False
  155. Attribute VB_PredeclaredId = True
  156. Attribute VB_Exposed = False
  157. Option Explicit
  158. ' Location of viewing eye.
  159. Private EyeR As Single
  160. Private EyeTheta As Single
  161. Private EyePhi As Single
  162. Private Const Dtheta = PI / 20
  163. Private Const Dphi = PI / 20
  164. Private Const Dr = 1
  165. ' Location of focus point.
  166. Private Const FocusX = 0#
  167. Private Const FocusY = 0#
  168. Private Const FocusZ = 0#
  169. Private Projector(1 To 4, 1 To 4) As Single
  170. Private TheGrid As FractalGrid3d
  171. Private Enum SurfaceTypes
  172.     surface_Mountain = 0
  173.     surface_Hill = 1
  174.     surface_Ridge = 2
  175.     surface_PeakedRidge = 3
  176.     surface_RuggedRidge = 4
  177.     surface_Random = 5
  178.     surface_Valley = 6
  179. End Enum
  180. Private SelectedSurface As SurfaceTypes
  181. Private SphereRadius As Single
  182. Private Const Amplitude3 = 2
  183. Private Const Xmin = -5
  184. Private Const Zmin = -5
  185. ' Return the Y coordinate for these X and
  186. ' Z coordinates.
  187. Private Function YValue(ByVal X As Single, ByVal Z As Single)
  188. Dim Y As Single
  189. Dim D As Single
  190. Dim d2 As Single
  191. Dim x1 As Single
  192. Dim x2 As Single
  193.     Select Case SelectedSurface
  194.         Case surface_Mountain
  195.             x1 = X + 0.5
  196.             D = 2 * (Amplitude3 - Sqr(x1 * x1 + Z * Z))
  197.             x2 = X - 0.5
  198.             d2 = 2 * (Amplitude3 - Sqr(x2 * x2 + Z * Z)) - 0.5
  199.             If D < d2 Then D = d2
  200.             If D < -Amplitude3 Then D = -Amplitude3
  201.             Y = D
  202.         Case surface_Hill
  203.             D = X * X + Z * Z
  204.             If D >= SphereRadius Then
  205.                 Y = 0
  206.             ElseIf Z < 0 Then
  207.                 Y = 0.75 * Sqr(SphereRadius - D)
  208.             Else
  209.                 Y = 0.75 * Sqr(SphereRadius - D) * (3 - Z) / 3
  210.             End If
  211.         Case surface_Ridge
  212.             Y = 2 * Cos(2 * PI / 10 * Z) * (5 - Abs(Z)) / 5 + 0.5 * Rnd
  213.         Case surface_PeakedRidge
  214.             Y = 2 * Cos(2 * PI / 10 * Z) * (5 - Abs(Z)) / 5 + 0.25 * Sin(2 * X) + 0.25 * Sin(1# * X + x1) + 0.5 * Rnd
  215.         Case surface_RuggedRidge
  216.             Y = 2 * Cos(2 * PI / 10 * Z) * (5 - Abs(Z)) / 5 + Rnd
  217.         Case surface_Random
  218.             Y = Rnd
  219.         Case surface_Valley
  220.             Y = -2 * Cos(2 * PI / 10 * Z) * (5 - Abs(Z)) / 5 + 0.25 * Sin(2 * X) + 0.25 * Sin(1# * X + x1) + 0.5 * Rnd
  221.             If Y < -1 Then Y = -1
  222.     End Select
  223.     YValue = Y
  224. End Function
  225. ' Project and display the data.
  226. Private Sub DrawData(pic As Object)
  227. Dim X As Single
  228. Dim Y As Single
  229. Dim Z As Single
  230. Dim S(1 To 4, 1 To 4) As Single
  231. Dim T(1 To 4, 1 To 4) As Single
  232. Dim ST(1 To 4, 1 To 4) As Single
  233. Dim PST(1 To 4, 1 To 4) As Single
  234.     MousePointer = vbHourglass
  235.     DoEvents
  236.     ' Make the data.
  237.     CreateData
  238.     ' Scale and translate so it looks OK in pixels.
  239.     m3Scale S, 35, -35, 1
  240.     m3Translate T, 230, 175, 0
  241.     m3MatMultiplyFull ST, S, T
  242.     m3MatMultiplyFull PST, Projector, ST
  243.     ' Transform the points.
  244.     TheGrid.ApplyFull PST
  245.     ' Prevent overflow errors when drawing lines
  246.     ' too far out of bounds.
  247.     On Error Resume Next
  248.     ' Display the data.
  249.     pic.Cls
  250.     TheGrid.RemoveHidden = (chkRemoveHidden.value = vbChecked)
  251.     TheGrid.Draw pic
  252.     pic.Refresh
  253.     MousePointer = vbDefault
  254. End Sub
  255. Private Sub cmdDraw_Click()
  256.     DrawData picCanvas
  257. End Sub
  258. Private Sub optSurface_Click(Index As Integer)
  259.     SelectedSurface = Index
  260.     DrawData picCanvas
  261.     picCanvas.SetFocus
  262. End Sub
  263. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  264.     Select Case KeyCode
  265.         Case vbKeyLeft
  266.             EyeTheta = EyeTheta - Dtheta
  267.         
  268.         Case vbKeyRight
  269.             EyeTheta = EyeTheta + Dtheta
  270.         
  271.         Case vbKeyUp
  272.             EyePhi = EyePhi - Dphi
  273.         
  274.         Case vbKeyDown
  275.             EyePhi = EyePhi + Dphi
  276.                 
  277.         Case Else
  278.             Exit Sub
  279.     End Select
  280.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  281.     DrawData picCanvas
  282. End Sub
  283. Private Sub Form_KeyPress(KeyAscii As Integer)
  284.     Select Case KeyAscii
  285.         Case Asc("+")
  286.             EyeR = EyeR + Dr
  287.         
  288.         Case Asc("-")
  289.             EyeR = EyeR - Dr
  290.         
  291.         Case Else
  292.             Exit Sub
  293.     End Select
  294.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  295.     DrawData picCanvas
  296. End Sub
  297. Private Sub Form_Load()
  298.     Randomize
  299.     ' Initialize the eye position.
  300.     EyeR = 10
  301.     EyeTheta = PI * 0.2
  302.     EyePhi = PI * 0.1
  303.     ' Initialize the projection transformation.
  304.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  305.     ' Project and draw the data.
  306.     Me.Show
  307.     DrawData picCanvas
  308. End Sub
  309. ' Create the surface.
  310. Private Sub CreateData()
  311. Const Dx = 1
  312. Const Dz = 1
  313. Const NumX = -2 * Xmin / Dx
  314. Const NumZ = -2 * Zmin / Dz
  315. Dim i As Integer
  316. Dim j As Integer
  317. Dim X As Single
  318. Dim Y As Single
  319. Dim Z As Single
  320. Dim level As Integer
  321. Dim Dy As Single
  322.     SphereRadius = (Xmin + 3 * Dx) * (Xmin + 3 * Dx)
  323.     Set TheGrid = New FractalGrid3d
  324.     TheGrid.SetBounds Xmin, Dx, NumX, Zmin, Dz, NumZ
  325.     X = Xmin
  326.     For i = 1 To NumX
  327.         Z = Zmin
  328.         For j = 1 To NumZ
  329.             Y = YValue(X, Z)
  330.             TheGrid.SetValue X, Y, Z
  331.             Z = Z + Dz
  332.         Next j
  333.         X = X + Dx
  334.     Next i
  335.     On Error Resume Next
  336.     level = CInt(txtLevel.Text)
  337.     If Err.Number <> 0 Then
  338.         txtLevel.Text = "3"
  339.         level = 3
  340.     End If
  341.     Dy = CSng(txtDy.Text)
  342.     If Err.Number <> 0 Then
  343.         txtDy.Text = "0.25"
  344.         Dy = 0.25
  345.     End If
  346.     TheGrid.GenerateSurface level, Dy
  347.     ' If this is the valley, flatten the bottom.
  348.     If SelectedSurface = surface_Valley Then
  349.         TheGrid.Flatten -1, 0.25, 0.25
  350.     End If
  351. End Sub
  352.